import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:tiktokadult/hj_page/video/view/video_menu_view.dart';
import 'package:video_player/video_player.dart';

import '../../hj_model/video_model.dart';
import '../../hj_utils/screen.dart';

class VideoFullPage extends StatefulWidget {
  final VideoPlayerController controller;
  final VideoModel? videoModel;
  final bool isAutoV; // 判断size，自动转竖屏
  final bool showMenu; //是否需要显示菜单栏，直播不需要展示菜单栏

  const VideoFullPage({
    super.key,
    required this.controller,
    this.videoModel,
    this.isAutoV = true,
    this.showMenu = true,
  });

  @override
  State<StatefulWidget> createState() {
    return _VideoFullPageState();
  }
}

class _VideoFullPageState extends State<VideoFullPage> with WidgetsBindingObserver {
  VideoPlayerController get controller => widget.controller;

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addObserver(this);
    if (controller.value.aspectRatio > 1 || !widget.isAutoV) {
      SystemChrome.setEnabledSystemUIMode(SystemUiMode.manual, overlays: [SystemUiOverlay.bottom]);
      SystemChrome.setPreferredOrientations([
        DeviceOrientation.landscapeLeft,
        DeviceOrientation.landscapeRight,
      ]);
    }
    WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
      setState(() {});
    });
  }

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    super.didChangeAppLifecycleState(state);
    if (state == AppLifecycleState.paused) {
      controller.pause();
    }
  }

  void backEvent() {
    SystemChrome.setEnabledSystemUIMode(SystemUiMode.manual, overlays: [SystemUiOverlay.bottom, SystemUiOverlay.top]);
    SystemChrome.setPreferredOrientations([
      DeviceOrientation.portraitUp,
    ]);
    Navigator.pop(context);
  }

  bool playerIsVertical = false;

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: () async {
        backEvent();
        return false;
      },
      child: Scaffold(
        backgroundColor: Colors.black,
        body: Container(
          child: Stack(
            alignment: Alignment.center,
            children: [
              Container(
                width: screen.screenWidth,
                height: screen.screenHeight,
              ),
              Hero(
                tag: "player",
                child: RotatedBox(
                  quarterTurns: playerIsVertical ? -1 : 0,
                  child: AspectRatio(
                    aspectRatio: controller.value.aspectRatio, // ?? resolutionSize?.aspectRatio ?? 1,
                    child: VideoPlayer(controller),
                  ),
                ),
              ),
              Positioned(
                bottom: 0,
                left: 0,
                right: 0,
                child: widget.showMenu
                    ? SizedBox(
                        width: screen.screenWidth,
                        height: screen.screenHeight,
                        child: VideoMenuView(
                          controller: controller,
                          isFull: true,
                          callback: backEvent,
                          videoModel: widget.videoModel,
                        ),
                      )
                    : SizedBox.shrink(),
              ),
              Align(
                alignment: Alignment.topLeft,
                child: InkWell(
                  onTap: backEvent,
                  child: Container(
                    width: 30,
                    height: 30,
                    margin: EdgeInsets.fromLTRB(5, screen.paddingTop + 5, 15, 15),
                    child: const Icon(
                      Icons.arrow_back_ios_sharp,
                      color: Colors.white,
                    ),
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }

  Widget buildRotateButton() {
    return Visibility(
      visible: true,
      child: Container(
        alignment: Alignment.centerRight,
        padding: const EdgeInsets.only(top: 60),
        child: InkWell(
          onTap: () {
            playerIsVertical = !playerIsVertical;
            setState(() {});
          },
          // child: Container(
          //   padding: const EdgeInsets.all(8),
          //   child: svgAssets(
          //     playerIsVertical ? AssetsSvg.VIDEO_IC_ROTATE_H : AssetsSvg.VIDEO_IC_ROTATE_V,
          //     width: 28,
          //     height: 28,
          //   ),
          // ),
        ),
      ),
    );
  }

  @override
  void dispose() {
    WidgetsBinding.instance.removeObserver(this);
    super.dispose();
  }
}
